package com.uenpay.im.ui.old.activity.dynamic;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qyx.android.weight.view.MyListView;
import com.uenpay.im.IMApplication;
import com.uenpay.im.R;
import com.uenpay.im.ui.old.activity.chat.ActivityHandle;
import com.uenpay.im.weight.MyDialog;
import com.uenpay.util.app.base.BaseActivity;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2015-12-17
 * @copyRight aiti
 * @function Dynamic history list
 */
public class DynamicHistoryActivity extends BaseActivity {

    private MyListView listview;
    private int page = 1;
    private DynamicHistoryAdapter dynamicHistoryAdapter = null;
    private LinearLayout add_more_layout;
    private ArrayList<Reply> replys = new ArrayList<Reply>();
    private DynamicHandle mDynamicHandle = new DynamicHandle();
    private View loading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isFullScreen = true;
        super.onCreate(savedInstanceState);
    }

    @Override
    public View bindView() {
        return null;
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_normal_list_has_footer_layout;
    }

    @Override
    public void initView(View view) {
        initView();
        initListener();
        getDynamicHistory();
    }

    protected void initView() {
        ((TextView) findViewById(R.id.title_textview)).setText(getResources().getString(R.string.main_msg));
        ((TextView) findViewById(R.id.title_right_tv)).setText(getResources().getString(R.string.clear));
        listview = (MyListView) findViewById(R.id.listview);
        add_more_layout = (LinearLayout) findViewById(R.id.add_more_layout);
        loading = findViewById(R.id.loading);
    }

    protected void initListener() {
        findViewById(R.id.title_right_layout).setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (replys != null && replys.size() > 0) {
                    deleteMsgDialog("0");
                }
            }
        });
        add_more_layout.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                page++;
                getDynamicHistory();
            }
        });
        listview.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Reply reply = (Reply) arg0.getAdapter().getItem(arg2);
                if (reply != null) {
                    deleteMsgDialog(reply.replyid);
                }
                return true;
            }
        });
        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Reply reply = (Reply) arg0.getAdapter().getItem(arg2);
                if (reply != null) {
                    Bundle bundle = new Bundle();
                    bundle.putString("dynamic_id", reply.dynamicid);
                    ActivityHandle.startActivity(DynamicHistoryActivity.this, DynamicDetailActivity.class, bundle, -1);
                }
            }
        });
    }

    private void deleteMsgDialog(final String reply_id) {

        MyDialog.Builder alertDialog = new MyDialog.Builder(DynamicHistoryActivity.this);
        alertDialog.setTitle(R.string.dialog_title);
        String content = "";
        if (reply_id.equals("0")) {
            content = getResources().getString(R.string.clear_all_dynamic_history);
        } else {
            content = getResources().getString(R.string.clear_one_dynamic_history);
        }
        alertDialog.setMessage(content);
        alertDialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                deleteReply(reply_id);
            }
        });

        alertDialog.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.create().show();
    }

    /**
     * 获取历史动态评论数据
     */
    private void getDynamicHistory() {
        loading.setVisibility(View.VISIBLE);
        mDynamicHandle.getDynamicHistoryReplys(page, new DynamicHandle.IGetDynamicHistoryReply() {

            @Override
            public void onGetDynamicHistoryReplyResult(int status, String msg, ArrayList<Reply> _replys, boolean isCache) {
                loading.setVisibility(View.GONE);
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    if (page == 1) {
                        if (!isCache) {
                            IMApplication.config.setDynamicUnreadMsgCount(0);
                        }
                        if (dynamicHistoryAdapter == null) {
                            dynamicHistoryAdapter = new DynamicHistoryAdapter(DynamicHistoryActivity.this, _replys);
                            listview.setAdapter(dynamicHistoryAdapter);
                        } else {
                            dynamicHistoryAdapter.setData(_replys);
                        }
                        replys = _replys;
                    } else {
                        if (_replys != null) {
                            if (_replys.size() < 1) {
                                add_more_layout.setVisibility(View.GONE);
                            } else {
                                replys.addAll(_replys);
                                dynamicHistoryAdapter.addData(replys);
                            }
                        }
                    }
                    if (page == 1 && replys != null && replys.size() > 0) {
                        findViewById(R.id.title_right_layout).setVisibility(View.VISIBLE);
                    }
                } else if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
            }
        });

    }

    /**
     * 删除动态历史消息
     *
     * @param reply_id
     */
    private void deleteReply(final String reply_id) {
        loading.setVisibility(View.VISIBLE);
        mDynamicHandle.deleteDynamicReplyHistory(reply_id, new DynamicHandle.IDeleteDynamicReplyHistoryListener() {

            @Override
            public void onDeleteDynamicReplyHisResult(int status, String msg) {
                loading.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(msg)) {
                    IMApplication.showToast(msg);
                }
                if (status == HttpInvokeResultCode.RESULT_OK) {
                    if (!reply_id.equals("0")) {
                        for (int i = 0, size = replys.size(); i < size; i++) {
                            if (replys.get(i).replyid.equals(reply_id)) {
                                replys.remove(i);
                                dynamicHistoryAdapter.notifyDataSetChanged();
                                break;
                            }
                        }
                    } else {
                        replys = null;
                        listview.setAdapter(null);
                        add_more_layout.setVisibility(View.GONE);
                    }
                }
            }
        });
    }
}
